//
//  HMWelcomeViewController.swift
//  Weibo-swift
//
//  Created by GD on 16/9/22.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit
import SDWebImage

class HMWelcomeViewController: UIViewController {
    
    
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var userImageView : UIImageView = {
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        view.layer.cornerRadius = view.size.width/2
        view.clipsToBounds = true
        //URL(string: imageURL)
        if let imageURL = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.profile_image_url{
            view.sd_setImage(with: URL(string: imageURL), placeholderImage: nil)
        }
        
        return view
    }()
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        
        if let name = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name{
            label.text = "欢迎回来 + \(name)"
        }else{
            
            label.text = "欢迎回来"
        }
        
        label.alpha = 0
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
    
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        startAnimation()
    }
    
    
    private func setupUI(){
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        userImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
        }
    }
    
    
    
    //开启动画
    private func startAnimation(){
        //更新头像约束
        userImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //弹簧动画
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { 
                self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animate(withDuration: 1.0, animations: { 
                    //显示  ---- 欢迎回来
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        //跳转到首页-----通过切换跟视图控制器.   登录成功要切换. 欢迎界面要切换!
                        NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotification), object: nil)
                })
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


}
